﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */


//Next: Change name to CloudServices and move some .Common code into this namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using MyData.ExpressionBuilder.BaseExtensions;
using MyData.ExpressionBuilder.Model;

namespace MyData.SharePoint
{
  //NEXT: Move to MyData.ExpressionBuilder
  public interface IItemList
  {
    List<MyData.ExpressionBuilder.Model.ListItem> Items { get; }
  }

  class ListItems
  {
    string _url = string.Empty;

    public ListItems(string webFullUrl)
    {
      _url = webFullUrl;
    }
    
    public FieldCollection GetSchema(string title)
    {
      FieldCollection fields = null;
      using (ClientContext clientContext = new ClientContext(_url))
      {
        List list = clientContext.Web.Lists.GetByTitle(title);
        clientContext.Load(list);
        fields = list.Fields;
        clientContext.Load(fields);
        clientContext.ExecuteQuery();
      }

      return fields;
    }
  }

  public class SharePointItemFields : SharePointItemBase
  {
    public SharePointItemFields(string url, string title)
    {
      this.Url = url;
      this.Title = title;
    }

    //Get the Title property from the ListTitle ModelItem
    public override string Title { get; set; }

    public List<MyData.ExpressionBuilder.Model.ListItem> Items
    {
      get { return GetFields(); }
    }

    //Use the SharePoint CSOM to get a List of Fields for an IExpressionBuilder
    private List<MyData.ExpressionBuilder.Model.ListItem> GetFields()
    {
      List<MyData.ExpressionBuilder.Model.ListItem> listFields = new List<MyData.ExpressionBuilder.Model.ListItem>();

      using (ClientContext clientContext = new ClientContext(this.Url))
      {
        if (this.Title != null)
        {
          List list = clientContext.Web.Lists.GetByTitle(this.Title);

          CamlQuery camlQuery = new CamlQuery();
          //How to OrderBy on server?
          camlQuery.ViewXml = "<View/>";

          ListItemCollection listItems = list.GetItems(camlQuery);
          clientContext.Load(list);

          IEnumerable<Field> fields = clientContext.LoadQuery(
                 list.Fields.Include(f => f.InternalName).Where(f => f.Hidden != true));
          clientContext.ExecuteQuery();

          IEnumerable<Field> sortedFields = from f in fields orderby f.InternalName select f;

          foreach (Field f in sortedFields)
          {
            listFields.Add(new MyData.ExpressionBuilder.Model.ListItem(f.InternalName));
          }
        }
      }

      return listFields;
    }
  }

  public class SharePointItems : IItemList
  {
    private string _expressionPath = string.Empty;
    private string _url = string.Empty;

    public SharePointItems(string url, string expressionPath)
    {
      _expressionPath = expressionPath;
      _url = url;
    }

    public List<MyData.ExpressionBuilder.Model.ListItem> Items
    {
      get
      {
        return this.ListTitles();
      }
    }

    //BUG: This is called  twice. Relates to all MyExpressionEditor
    private List<MyData.ExpressionBuilder.Model.ListItem> ListTitles()
    {
      List<MyData.ExpressionBuilder.Model.ListItem> nameValues = 
        new List<MyData.ExpressionBuilder.Model.ListItem>();

      //GenericList is the default for this version
      BaseType baseType = BaseType.GenericList;

      if (_expressionPath == "ModelItem.LibraryTitle")
        baseType = BaseType.DocumentLibrary;

      using (ClientContext clientContext = new ClientContext(_url))
      {
        ListCollection lists = clientContext.Web.Lists;
        clientContext.Load(lists);

        try
        {
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
          clientContext.ExecuteQuery();
        }
        finally
        {
          System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        var items = from l in lists.ToList() where !l.Hidden && l.BaseType == baseType orderby l.Title select l;

        foreach (var l in items)
        {
          nameValues.Add(new MyData.ExpressionBuilder.Model.ListItem(l.Title.Replace(" ", string.Empty), l.Title));
        }
      }

      return nameValues;
    }
  }
}
